import com.khh.dao.AddressDao;
import com.khh.entity.Address;
import com.khh.entity.User;
import com.khh.mapper.UserMapper;
import com.khh.mybatis.MybatisHelp;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;

import java.util.List;

/**
 * Created by webrx on 2017-08-31.
 */
public class test {
    @Test
    public void two1(){
        //二级缓存  注解不太好使
        MybatisHelp mh = new MybatisHelp();
        SqlSessionFactory sf = mh.getSf();
        System.out.println(sf);
        //1.
        SqlSession ss1 = sf.openSession();
        UserMapper uu1 = ss1.getMapper(UserMapper.class);
        List<User> aa1 = uu1.queryAllByZhu();
        System.out.println(aa1);
        ss1.commit();

        //2.
        SqlSession ss2 = sf.openSession();
        UserMapper uu2 = ss2.getMapper(UserMapper.class);
        List<User> aa2 = uu2.queryAllByZhu();
        System.out.println(aa2);
    }
    @Test
    public void two(){
        //二级缓存
        MybatisHelp mh = new MybatisHelp();
        SqlSessionFactory sf = mh.getSf();
        System.out.println(sf);

        //配置
        //1.
        SqlSession s1 = sf.openSession();
        UserMapper u1 = s1.getMapper(UserMapper.class);
        List<User> a1 = u1.queryAll();
        System.out.println(a1);
        s1.commit();

        //2.
        SqlSession s2 = sf.openSession();
        UserMapper u2 = s2.getMapper(UserMapper.class);
        List<User> a2 = u2.queryAll();
        System.out.println(a2);



    }

    //缓存 默认一级缓存 (会话级)  二级需要配置
    @Test
    public void  d(){
        //会话级
        MybatisHelp mh = new MybatisHelp();
        SqlSession s = mh.getSs();
        UserMapper sm = s.getMapper(UserMapper.class);

        List<User> aaa = sm.queryAll();
        //s.clearCache();  清除会话级别缓存
        List<User> bbb = sm.queryAll();
        System.out.println(aaa==bbb);
        //s.clearCache();



    }


    //懒加载
    @Test
    public void abc(){
       //没有实现类
        MybatisHelp mh = new MybatisHelp();
        SqlSession s = mh.getSs();
        UserMapper m = s.getMapper(UserMapper.class);

        User list = m.queryByIdLazy(3);
        System.out.println(list.getUage());
        System.out.println("---------订单信息--------");
        List<Address> os = list.getAddresses();
        for (Address a:os ) {
            System.out.println(a);
        }
    }

    //自动生成mepper entity 测试
    @Test
    public void ab(){
        AddressDao ad = new AddressDao();
        Address list = ad.selectByPrimaryKey(10);
        System.out.println(list.getAname());
    }
    @Test
    public void a(){

        AddressDao ad = new AddressDao();
        Address addr = new Address();
        addr.setAaddress("北京");
        addr.setAname("小李");
        addr.setAphone("1231");
        addr.setAuid(3);
        int n = ad.insert(addr);
        System.out.println(n);
    }
}
